package com.hands.sample.service.impl;

import  com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.model.canfulfill.*;
import com.hands.commen.entity.wx.Base64Util;
import com.hands.sample.config.UriConfig;
import com.hands.sample.httpHelper.HttpClientHelper;
import net.sf.json.JSONObject;
//import org.apache.logging.log4j.core.util.SystemClock;
//import redis.clients.jedis.Jedis;

import java.io.UnsupportedEncodingException;

/**
 * TODO：智慧家庭等处理服务入口
 * DATE：2019-07-16
 * By  ：YanPeixin
 */
public class WisdomHomeService {

    /**
     * 请求音箱
     * @param handlerInput
     * @param speek
     * @return
     */
    public static JSONObject requestSound(HandlerInput handlerInput,String speek){
        String returnMsg = "";
        String by64 = null;
        String deviceid="";
        try {
            by64 = Base64Util.encode(speek.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String devMacId = handlerInput.getRequestEnvelope().getContext().getSystem().getDevice().getDeviceId(); //音箱设备id
        //获取音响设备信息
        String soundHostData = HttpClientHelper.doGet(String.format(UriConfig.soundHostUrl, devMacId));//请求音箱设备信息地址
        JSONObject jo = JSONObject.fromObject(soundHostData);
        String userId = "";
        String reCode = jo.get("code").toString();
        String devtype = "";
        if (reCode.equals("true")) {
            JSONObject sh = JSONObject.fromObject(jo.get("data"));
            userId = sh.get("userid").toString();
            deviceid= devMacId;//sh.get("deviceid").toString();
            devtype = sh.get("devip").toString();
        } else {
            JSONObject json = new JSONObject();
            json.put("sucess","true");
            json.put("speek","设备未添加绑定");
            return json;
        }
        userId=userId.substring(0,userId.indexOf("_"));
        if (devtype.equals("githome")||devtype.equals("HaierHotel"))//git-jihong.com
            returnMsg = null;  //SocketClient.requestNewSocket(userId, "5;513", by64,deviceid);//访问云脑服务器 ， 判断用户语义是否为控制智能家居
        else if (devtype.equals("git-jihong.com"))//git-jihong.com
        {
            returnMsg = null;//SocketClient.requestGitSocket(userId, "5;513", by64,deviceid);
        }else
            returnMsg = null;//SocketClient.requestHomeSocket(userId, "5;513", by64,deviceid);//访问云脑服务器 ， 判断用户语义是否为控制智能家居
        JSONObject json = JSONObject.fromObject(returnMsg);
        return json;
    }


    /**
     * 回传用于响应技能，yes：开发技能处理 , no：抛给音箱处理
     * @param jsonData
     * @param speek
     * @return
     */
    public static CanFulfillIntent responeCanFulfillSlot(JSONObject jsonData, String speek, String domainArea,String answer, HandlerInput handlerInput){
        //连接本地的 Redis 服务
        CanFulfillSlot CanUnderstand ;
        CanFulfillIntent canFulfillIntent;
        String sucess=jsonData.getString("sucess");
        ///这个是设备领域的处理
        //如果为2014则返回false音箱自己处理，反之是由技能进行处理
        if(!sucess.equals("true")){
            CanUnderstand = CanFulfillSlot.builder().withCanUnderstand(CanUnderstandSlotValues.NO).withCanFulfill(CanFulfillSlotValues.NO).build();
            canFulfillIntent =
                    CanFulfillIntent.builder()
                            .withCanFulfill(CanFulfillIntentValues.NO).putSlotsItem("slotName1", CanUnderstand).build();
        }else
        {
            try {
                //logC.teststate=jsonData.toString();
                //logC.writeFile("","家庭主机返回的数据："+jsonData.toString());
                JedisUtil.getInstance().set( handlerInput.getRequestEnvelope().getContext().getSystem().getDevice().getDeviceId()+"speek",jsonData.toString());
            }  catch (Exception ex)
            {
               // logC.writeFile("","redis出错："+ex.toString());
            }
            CanUnderstand = CanFulfillSlot.builder().withCanUnderstand(CanUnderstandSlotValues.YES).withCanFulfill(CanFulfillSlotValues.YES).build();
            canFulfillIntent =
                    CanFulfillIntent.builder()
                            .withCanFulfill(CanFulfillIntentValues.YES).putSlotsItem("slotName1", CanUnderstand).build();

        }
        return  canFulfillIntent;
    }

}
